using AutoMapper;
using BCVP.Net8.Common.app;
using BCVP.Net8.Common.appsettings;
using BCVP.Net8.Common.cache;
using BCVP.Net8.Common.convert;
using BCVP.Net8.Extension.options;
using BCVP.Net8.Model;
using BCVP.Net8.Model.logrecode;
using BCVP.Net8.Service.Base;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;

namespace BCVP.Net8.Controllers
{
    [ApiController]
    [Route("[controller]/[action]")]
    [Authorize("Permission")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;
        private readonly IMapper _mapper;
        public IBaseService<Role, RoleVo> _baseService { get; set; }
        private readonly IOptions<RedisOptions> _redisOptions;
        private readonly ICaching _caching;
        private readonly IBaseService<AuditSqlLog, AuditSqlLogVo> _auditSqlLogService;
        private readonly IHttpContextAccessor _httpContextAccessor;

        public WeatherForecastController(ILogger<WeatherForecastController> logger,
            IMapper mapper,
            IBaseService<Role, RoleVo> baseService,
            IOptions<RedisOptions> redisOptions,
            ICaching caching,
            IBaseService<AuditSqlLog, AuditSqlLogVo> auditSqlLogService,
            IHttpContextAccessor httpContextAccessor)
        {
            _logger = logger;
            _mapper = mapper;
            _baseService = baseService;
            _redisOptions = redisOptions;
            _caching = caching;
            _auditSqlLogService = auditSqlLogService;
            _httpContextAccessor = httpContextAccessor;
        }


        [HttpGet(Name = "GetWeatherForecast")]
        public async Task<object> Get()
        {
            var roleList = await _baseService.Query();

            return roleList;
        }


        [HttpGet(Name = "GetWeatherForecastById")] // or [HttpGet("one")]
        public async Task<IActionResult> GetOne()
        {
            var roleList = await _auditSqlLogService.Query();
            return Ok(roleList);
        }

        [HttpGet(Name = "GetWeatherForecastByName")] // or [HttpGet("one")]
        public async Task<object> GetTwo()
        {
            TimeSpan timeSpan = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            var id = timeSpan.TotalSeconds.ObjToLong();
            await _auditSqlLogService.AddSplit(new AuditSqlLog()
            {
                Id = id,
                DateTime = Convert.ToDateTime("2025-08-20"),
            });

            var rltList = await _auditSqlLogService.QuerySplit(d => d.DateTime >= Convert.ToDateTime("2025-08-20"));

            Console.WriteLine("api request end...");
            return rltList;
        }

        [HttpGet(Name = "GetWeatherForecastByHttpContext")]
        public async Task<object> GetThree()
        {
            Console.WriteLine("api reuquest begin...");
            var httpContext = _httpContextAccessor.HttpContext?.User.Claims.ToList();

            foreach (var item in httpContext)
            {
                await Console.Out.WriteLineAsync($"{item.Type} : {item.Value}");
            }

            var rltList = await _auditSqlLogService.QuerySplit(d => d.DateTime >= Convert.ToDateTime("2025-08-20"));

            Console.WriteLine("api request end...");
            return rltList;
        }

    }
}
